////////////////////////////////////
// Win32WrapperClass			  //
//                                //
// File: Win32WrapperClass.cpp    // 
// Desc: Declaration of the       //
//			Win32WrapperClass     //
//			class.				  //
////////////////////////////////////


#include "Win32WrapperClass.h"

///////////////////////////////////////////////
// Static Win32WrapperClass::stWinProc
///////////////////////////////////////////////

LRESULT CALLBACK Win32WrapperClass::stWinProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	// This does two things:
	// 1.) If the window is being created, it calls SetWindowLong on the window to store the 
	//		pointer to the window.
	// 2.) Otherwise, it gets the pointer to the window and calls that instance's WinProc

	Win32WrapperClass* pWnd;

	if (uMsg == WM_NCCREATE) {
		// Set the pointer
		SetWindowLong(hWnd, GWL_USERDATA, (long)((LPCREATESTRUCT(lParam))->lpCreateParams));
	}

	// Get the pointer
	pWnd = (Win32WrapperClass *)GetWindowLong(hWnd, GWL_USERDATA);

	// If we got it, call the windows message handler, otherwise call the default
	if (pWnd)	return pWnd->WinProc(hWnd, uMsg, wParam, lParam);
	else		return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


///////////////////////////////////////////////
// Win32WrapperClass::Create
///////////////////////////////////////////////

BOOL Win32WrapperClass::Create() {
	// Create the window

	WNDCLASSEX WndCls;

	// Create the application window
	WndCls.cbSize        = sizeof(WNDCLASSEX);
	WndCls.style         = 0;
	WndCls.lpfnWndProc   = stWinProc;
	WndCls.cbClsExtra    = 0;
	WndCls.cbWndExtra    = 0;
	WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndCls.lpszMenuName  = NULL;
	WndCls.lpszClassName = m_pClassName;
	WndCls.hInstance     = m_hInstance;
	WndCls.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	// Register the application
	RegisterClassEx(&WndCls);


	// send the this pointer as the window creation parameter
	m_hWnd = CreateWindowEx (WS_EX_CLIENTEDGE,
		m_pClassName,m_pWindowTitle,
		m_dwStyles,
		m_pRect->left,
		m_pRect->top,
		m_pRect->right-m_pRect->left,
		m_pRect->bottom-m_pRect->top,
		NULL,NULL,
		m_hInstance,
		(void *)this);
	return (m_hWnd != NULL);
}


///////////////////////////////////////////////
// Win32WrapperClass::Show
///////////////////////////////////////////////

BOOL Win32WrapperClass::Show() {

	// Show the window
	ShowWindow(m_hWnd,SW_SHOW);
	UpdateWindow(m_hWnd);

	return true;
}

///////////////////////////////////////////////
// Win32WrapperClass::Win32WrapperClass
///////////////////////////////////////////////

Win32WrapperClass::Win32WrapperClass() {
	// default, does nothing
}

///////////////////////////////////////////////
// Win32WrapperClass::Win32WrapperClass
///////////////////////////////////////////////

Win32WrapperClass::Win32WrapperClass(char *ClassName, char *WindowTitle, DWORD dwStyles, RECT *rect, HINSTANCE hInstance) {
	m_pClassName	= ClassName;
	m_pWindowTitle	= WindowTitle;
	m_pRect			= rect;
	m_hInstance		= hInstance;
	m_dwStyles		= dwStyles;

	RECT desktopRect;
	GetWindowRect(GetDesktopWindow(),&desktopRect);

	int wWidth	=	m_pRect->right	-	m_pRect->left;
	int wHeight	=	m_pRect->bottom	-	m_pRect->top;

	m_pRect->left	=	((desktopRect.right		-	desktopRect.left)	-	wWidth)		/2;
	m_pRect->right	=	((desktopRect.right		-	desktopRect.left)	+	wWidth)		/2;
	m_pRect->top	=	((desktopRect.bottom	-	desktopRect.top)	-	wHeight)	/2;
	m_pRect->bottom	=	((desktopRect.bottom	-	desktopRect.top)	+	wHeight)	/2;
}


///////////////////////////////////////////////
// Win32WrapperClass::~Win32WrapperClass
///////////////////////////////////////////////

Win32WrapperClass::~Win32WrapperClass () {

	// Destroy the render window
	if ( m_hWnd ) DestroyWindow( m_hWnd );
	m_hWnd = NULL;
}


///////////////////////////////////////////////
// Win32WrapperClass::WinProc
///////////////////////////////////////////////

LRESULT CALLBACK Win32WrapperClass::WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	// Define our true window processor here

	switch(uMsg)
	{
		// If the user wants to close the application
	case WM_DESTROY:
		// then close it
		PostQuitMessage(WM_QUIT);
		break;
	default:
		// Process the left-over messages
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}

	// If something was not done, let it go
	return 0;
}


